package com.shaokel.eduservice.service.impl;

import com.shaokel.eduservice.entity.EduCourse;
import com.shaokel.eduservice.entity.EduCourseDescription;
import com.shaokel.eduservice.entity.vo.CourseInfoVo;
import com.shaokel.eduservice.entity.vo.CoursePublishVo;
import com.shaokel.eduservice.mapper.EduCourseMapper;
import com.shaokel.eduservice.service.EduChapterService;
import com.shaokel.eduservice.service.EduCourseDescriptionService;
import com.shaokel.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaokel.eduservice.service.EduVideoService;
import com.shaokel.exception.shaokelException;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 课程 服务实现类
 *
 * @author 少丶客
 * @since 2020-06-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    //删除课程
    @Override
    public void deleteCourse(String courseId) {
        //1.删除小节
        eduVideoService.removeVideoByCourseId(courseId);
        //2.删除章节
        eduChapterService.removeChapterByCourseId(courseId);
        //3.删除描述
        eduCourseDescriptionService.removeById(courseId);
        //4.删除课程
        int result = baseMapper.deleteById(courseId);
        if(result==0){
            throw new shaokelException(20001,"删除失败");
        }

    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改课程表
            EduCourse eduCourse = new EduCourse();
            BeanUtils.copyProperties(courseInfoVo,eduCourse);
            int result = baseMapper.updateById(eduCourse);
            if(result == 0){
                throw new shaokelException(20001,"修改课程信息失败");
        }

        //2.修改描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        description.setId(courseInfoVo.getId());
        eduCourseDescriptionService.updateById(description);
    }

    //根据课程id查询课程基本信息接口
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //2.查询描述表
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;
    }

    //添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int result = baseMapper.insert(eduCourse);
        if(result<=0){
            throw new shaokelException(20001,"添加课程失败");
        }

        //获取添加之后的id
        String cid = eduCourse.getId();

        //向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        //设置Description的id为课程id
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);

        return cid;
    }
}
